import { reqGoodsInfo, reqAddOrUpdateShopCart } from '../../api'
// 引入封装的游客临时身份的模块，用于生成一个随机的字符串
import {getUUID} from '../../utils/uuid_token'

const state = {
  goodsInfo: {},
  // 游客临时身份
  uuid_token: getUUID(),
}
const mutations = {
  GETDETAILLIST(state, goodInfo) {
    state.goodsInfo = goodInfo
  },
  
}
const actions = {
  //获取详情的数据
  async getDetailList({ commit }, skuId) {
    let result = await reqGoodsInfo(skuId)
    // console.log(result);
    if (result.code == 200) {
      commit('GETDETAILLIST', result.data)
    }
  },
  // 将产品添加到购物车中
  async addOrUpdateShopCart({commit}, {skuId, skuNum}){
    let result = await reqAddOrUpdateShopCart(skuId, skuNum)
    if (result.code == 200){
      return "ok"
    }else{
      return Promise.reject(new Error('fail'))
    }
  }
}

// 简化数据
const getters = {
  categoryView(state) {
    // 初始的时候state中的goodsInfo是个空对象，为了防止出现警告，给下面的返回值加上 ||{}
    return state.goodsInfo.categoryView || {}
  },
  skuInfo(state){
    return state.goodsInfo.skuInfo || {}
  },
  spuSaleAttrList(state){
    return state.goodsInfo.spuSaleAttrList || []
  }
}

export default {
  state,
  mutations,
  actions,
  getters,
}
